/*
 * JTetrisContainer.java
 *
 * This work is free software; you can redistribute it and/or
 * modify it under the terms of the Apache License as published 
 * by the Apache Software Foundation.
 *
 * This work is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 * Apache License for more details.
 *
 * Copyright (c) 2011 Tran Hoang Anh (tranhoanganh89@gmail.com). 
 * All rights reserved.
 */
package net.hoanganh.jbraintetris;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * The user interface component for tetris game.
 */
@SuppressWarnings("serial")
public class JTetrisContainer extends Container {
	private TetrisBoard board;
	private PreviewBoard preview;
	private JLabel scoreLbl;
	private JLabel score;
	private JLabel levelLbl;
	private JLabel level;
	private JButton control;

	public JTetrisContainer(JTetrisConfig config) {
		super();
		
		// Add components
		board = new TetrisBoard(config.BOARD_SQUARE_WIDTH,
				config.BOARD_SQUARE_HIEGHT);
		board.setPreferredSize(new Dimension(config.BOARD_WIDTH,
				config.BOARD_HIEGHT));

		preview = new PreviewBoard(config.PREVIEW_SQUARE_WIDTH,
				config.PREVIEW_SQUARE_HIEGHT);
		preview.setPreferredSize(new Dimension(config.PREVIEW_WIDTH,
				config.PREVIEW_HIEGHT));

		JPanel infoPanel = new JPanel(new BorderLayout());
		scoreLbl = new JLabel("Score");
		scoreLbl.setFont(scoreLbl.getFont().deriveFont(Font.BOLD));
		score = new JLabel();
		levelLbl = new JLabel("Level");
		levelLbl.setFont(levelLbl.getFont().deriveFont(Font.BOLD));
		level = new JLabel();
		control = new JButton("Start");
		control.setFocusable(false);
		
		JLabel authorLbl = new JLabel("Tran Hoang Anh");
		authorLbl.setForeground(Color.blue);
		JLabel copyRightLbl = new JLabel("2011");
		copyRightLbl.setForeground(Color.blue);
		
		scoreLbl.setAlignmentX(Component.CENTER_ALIGNMENT);
		score.setAlignmentX(Component.CENTER_ALIGNMENT);
		levelLbl.setAlignmentX(Component.CENTER_ALIGNMENT);
		level.setAlignmentX(Component.CENTER_ALIGNMENT);
		control.setAlignmentX(Component.CENTER_ALIGNMENT);
		authorLbl.setAlignmentX(Component.CENTER_ALIGNMENT);
		copyRightLbl.setAlignmentX(Component.CENTER_ALIGNMENT);
		
		infoPanel.add(preview, BorderLayout.NORTH);
		
		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		panel.add(Box.createRigidArea(new Dimension(0, config.INFO_GAP)));
		panel.add(scoreLbl);
		panel.add(score);
		panel.add(Box.createRigidArea(new Dimension(0, config.INFO_GAP)));
		panel.add(levelLbl);
		panel.add(level);
		panel.add(Box.createRigidArea(new Dimension(0, config.INFO_GAP)));
		panel.add(control);	
		panel.add(Box.createVerticalGlue());
		panel.add(authorLbl);
		panel.add(copyRightLbl);
		panel.add(Box.createRigidArea(new Dimension(0, config.INFO_GAP)));
		infoPanel.add(panel, BorderLayout.CENTER);

		setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
		add(board);
		add(Box.createRigidArea(new Dimension(config.FRAME_BORDER_SIZE, 0)));
		add(infoPanel);
	}

	/**
	 * @return the board
	 */
	public TetrisBoard getBoard() {
		return board;
	}

	/**
	 * @return the preview
	 */
	public PreviewBoard getPreview() {
		return preview;
	}

	/**
	 * @return the control
	 */
	public JButton getControlButton() {
		return control;
	}

	public void setScore(int score) {
		this.score.setText(Integer.toString(score));
	}

	public void setLevel(int level) {
		this.level.setText(Integer.toString(level));
	}
}
